﻿using HtmlAgilityPack;
using Newtonsoft.Json;
using Serilog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using XjjXmm.FrameWork.Cache;
using XjjXmm.FrameWork.ToolKit;
using XjjXmm.Spider.biquge;

namespace XjjXmm.Spider
{
    internal class ExportService
    {
        //https://www.zhihu.com/question/359676590/answer/1615531107


        private readonly ISqlSugarClient _sqlSugarClient;


        public ExportService(ISqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;

        }


        /// <summary>
        /// <book>
        ///     <title>ss</title>
        ///     <author>aaa</author>
        ///     <chapters>
        ///         <chapter>
        ///             <title>ddd</title>
        ///             <content>ddd</content>
        ///         </chapter>
        ///     </chapters>
        /// </book>
        /// </summary>
        /// <returns></returns>
        public async Task Run()
        {

            await Write();

        }


        private async Task<string> Write()
        {
            var chaptes = await _sqlSugarClient.Queryable<Chapter>().Where(t => t.NovelId == 1).ToListAsync();

            var file = $"1/1.xml";

            if (File.Exists(file))
            {
                File.Delete(file);
            }

            await File.AppendAllTextAsync(file, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            await File.AppendAllTextAsync(file, System.Environment.NewLine);
            await File.AppendAllTextAsync(file, "<book>");
            await File.AppendAllTextAsync(file, System.Environment.NewLine);
            await File.AppendAllTextAsync(file, "<chapters>");
            await File.AppendAllTextAsync(file, System.Environment.NewLine);
            //XmlReader reader = XmlReader.Create()
            foreach (var chapter in chaptes)
            {
                await File.AppendAllTextAsync(file, "<chapter>");
                await File.AppendAllTextAsync(file, System.Environment.NewLine);
                await File.AppendAllTextAsync(file, $"<title>{chapter.Title}</title>");

                await File.AppendAllTextAsync(file, System.Environment.NewLine);
                await File.AppendAllTextAsync(file, $"<content>");
                await File.AppendAllTextAsync(file, System.Environment.NewLine);
                await File.AppendAllTextAsync(file, "<![CDATA[");
                await File.AppendAllTextAsync(file, System.Environment.NewLine);
                await File.AppendAllTextAsync(file, $"{ProcessContent(chapter.Content)}");
                await File.AppendAllTextAsync(file, System.Environment.NewLine);
                await File.AppendAllTextAsync(file, "]]>");
                await File.AppendAllTextAsync(file, System.Environment.NewLine);
                await File.AppendAllTextAsync(file, $"</content>");

                await File.AppendAllTextAsync(file, System.Environment.NewLine);
                await File.AppendAllTextAsync(file, "</chapter>");
                await File.AppendAllTextAsync(file, System.Environment.NewLine);
                //await File.AppendAllTextAsync($"1/{chapter.Title}.txt", chapter.Content);
                //await File.AppendAllTextAsync("1/1.txt", chapter.Title);
                //await File.AppendAllTextAsync("1/1.txt", System.Environment.NewLine);
                //await File.AppendAllTextAsync("1/1.txt", chapter.Content);
            }
            await File.AppendAllTextAsync(file, "</chapters>");
            await File.AppendAllTextAsync(file, System.Environment.NewLine);
            await File.AppendAllTextAsync(file, "</book>");

            await CompressKit.Compress7z(file);
            return file; 
        }
       
        
        public void ReadBook()
        {
            var file = $"1/1.xml";

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            settings.IgnoreWhitespace = true;

            using (XmlReader rd = XmlReader.Create(file, settings))
            {
                rd.MoveToContent();
                while (rd.Read())
                {
                    if (rd.NodeType == XmlNodeType.Element && rd.Name == "title")
                    {
                        Console.WriteLine(rd.ReadElementContentAsString());
                       
                    }

                    if (rd.NodeType == XmlNodeType.Element && rd.Name == "content")
                    {
                        Console.WriteLine(rd.ReadElementContentAsString());

                    }

                }
            }
        }


        private string ProcessContent(string content)
        {
              return content.Replace("&nbsp;", "");
        }
    }
}
